package com.zhengshang.api.controller.goodsshop;

import com.zhengshang.api.domain.AppNavigate;
import com.zhengshang.api.domain.AppThirdColumn;
import com.zhengshang.api.domain.AppThirdColumnCategory;
import com.zhengshang.api.service.IAppBillProductService;
import com.zhengshang.api.service.IAppNavigateService;
import com.zhengshang.api.service.IAppThirdColumnCategoryService;
import com.zhengshang.api.service.IAppThirdColumnService;
import com.zhengshang.common.core.controller.BaseController;
import com.zhengshang.common.core.domain.AjaxResult;
import com.zhengshang.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(tags = "影视/音乐/教育产品接口")
@RestController
@RequestMapping("/bill")
public class ThirdBillProductController extends BaseController {

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private IAppBillProductService appBillProductService;

    @Autowired
    private IAppThirdColumnService appThirdColumnService;

    @Autowired
    private IAppThirdColumnCategoryService appThirdColumnCategoryService;

    @Autowired
    private IAppNavigateService appNavigateService;


    /**
     * 影视视频/音乐会员/教育会员
     *
     * @param navigateId 11.影音会员，12.音乐会员，13.在线教育
     * @return
     */
    @ApiOperation(value = "分类列表")
    @GetMapping("/thirdColumnList")
    public AjaxResult thirdColumnList(Long navigateId) {
        List<Integer> productTypes = Lists.newArrayList();
        productTypes.add(11);
        productTypes.add(12);
        productTypes.add(13);
        productTypes.add(16);
        productTypes.add(17);
        productTypes.add(18);
        productTypes.add(19);
        AppNavigate appNavigate = appNavigateService.selectAppNavigateById(navigateId);
        Integer productType = appNavigate.getProductType();
        if (!productTypes.contains(productType)) {
            return AjaxResult.error("类型不支持");
        }
        AppThirdColumn appThirdColumn = new AppThirdColumn();
        appThirdColumn.setThirdColumnType(productType);
        appThirdColumn.setThirdColumnState("1");
        List<AppThirdColumn> thirdColumnList = appThirdColumnService.selectAppThirdColumnList(appThirdColumn);
        String ossBaseUrl = configService.selectConfigByKey("sys.oss.file.header");
        for (AppThirdColumn thirdColumn : thirdColumnList) {
            thirdColumn.setThirdColumnIcon(ossBaseUrl + thirdColumn.getThirdColumnIcon());
        }
        Map<String, Object> map = new HashMap<>(5);
        map.put("navigateDesc", appNavigate.getNavigateDesc());
        map.put("thirdColumnList", thirdColumnList);
        return AjaxResult.success(map);
    }


    /**
     * 影视教育
     *
     * @return
     */
    @ApiOperation(value = "产品列表")
    @GetMapping("/queryProductByThirdColumnId")
    public AjaxResult queryProductByThirdColumnId(Long thirdColumnId) {
        AppThirdColumn appThirdColumn = appThirdColumnService.selectAppThirdColumnById(thirdColumnId);
        Map<String, Object> data = new HashMap<>();
        List<Map<String, Object>> list = Lists.newArrayList();
        AppThirdColumnCategory queryThirdColumnCategory = new AppThirdColumnCategory();
        queryThirdColumnCategory.setThirdColumnId(thirdColumnId);
        List<AppThirdColumnCategory> thirdColumnCategoryList = appThirdColumnCategoryService.selectAppThirdColumnCategoryList(queryThirdColumnCategory);
        for (AppThirdColumnCategory thirdColumnCategory1 : thirdColumnCategoryList) {
            Long thirdColumnCategoryId = thirdColumnCategory1.getThirdColumnCategoryId();
            String thirdColumnCategoryName = thirdColumnCategory1.getThirdColumnCategoryName();
            Map<String, Object> queryMap = new HashMap<>();
            queryMap.put("thirdColumnId", thirdColumnId);
            queryMap.put("thirdColumnCategoryId", thirdColumnCategoryId);
            List<Map> mapList = appBillProductService.selectAppBillProductList1(queryMap);
            Map<String, Object> columnMap = new HashMap<>();
            columnMap.put("categoryName", thirdColumnCategoryName);
            columnMap.put("productList", mapList);
            list.add(columnMap);
        }
        data.put("list", list);
        data.put("thirdColumnDesc", appThirdColumn.getThirdColumnDesc());
        data.put("formType", appThirdColumn.getFormType());
        return AjaxResult.success(data);
    }

    /**
     * 麦当劳/肯德基具体产品
     *
     * @return
     */
    @ApiOperation(value = "产品列表")
    @GetMapping("/queryCouponProductList")
    public AjaxResult queryCouponProductList(Long thirdColumnId) {
        String ossBaseUrl = configService.selectConfigByKey("sys.oss.file.header");
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("thirdColumnId", thirdColumnId);
        List<Map> list = appBillProductService.selectAppBillProductList1(queryMap);
        list.forEach(map -> {
            String productImg = (String) map.get("productImg");
            if (!productImg.contains("http")) {
                map.put("productImg", ossBaseUrl + productImg);
            }
        });
        return AjaxResult.success(list);
    }

}
